package com.framework.entity;

import com.framework.util.Const;
import com.framework.util.PageData;
import com.framework.util.Tools;

public class Page
{
	private int showCount; // 每页显示记录数
	private int totalPage; // 总页数
	private int totalResult; // 总记录数
	private int currentPage; // 当前页
	private int currentResult; // 当前记录起始索引
	private boolean entityOrField; // true:需要分页的地方，传入的参数就是Page实体；false:需要分页的地方，传入的参数所代表的实体拥有Page属性
	private String page_html; // 最终页面显示的底部翻页导航，详细见：getPage_html();
	private PageData pd = new PageData();

	public Page()
	{
		try
		{
			this.showCount = Integer.parseInt(Tools.readTxtFile(Const.PAGE));
		} catch (Exception e)
		{
			this.showCount = 15;
		}
	}

	public int getTotalPage()
	{
		if (totalResult % showCount == 0)
			totalPage = totalResult / showCount;
		else
			totalPage = totalResult / showCount + 1;
		return totalPage;
	}

	public void setTotalPage(int totalPage)
	{
		this.totalPage = totalPage;
	}

	public int getTotalResult()
	{
		return totalResult;
	}

	public void setTotalResult(int totalResult)
	{
		this.totalResult = totalResult;
	}

	public int getCurrentPage()
	{
		if (currentPage <= 0)
			currentPage = 1;
		if (currentPage > getTotalPage())
			currentPage = getTotalPage();
		return currentPage;
	}

	public void setCurrentPage(int currentPage)
	{
		this.currentPage = currentPage;
	}

	public String getPage_html()
	{
		StringBuffer sb = new StringBuffer();
		if (totalResult > 0)
		{
			sb.append("<div class=\"dataTables_paginate paging_simple_numbers\">\n<ul class=\"pagination\">\n");
			if (currentPage == 1)
			{
				sb.append("	<li class=\"paginate_button disabled\"><a>首页</a></li>\n");
				sb.append("	<li class=\"paginate_button disabled\"><a>上页</a></li>\n");
			} else
			{
				sb.append("	<li class=\"paginate_button\"><a onclick=\"nextPage(1)\">首页</a></li>\n");
				sb.append("	<li class=\"paginate_button\"><a onclick=\"nextPage("+(currentPage-1)+")\">上页</a></li>\n");
			}
			int showTag = 5;// 分页标签显示数量
			int startTag = 1;
			if (currentPage > showTag)
			{
				startTag = currentPage - 1;
			}
			int endTag = startTag + showTag - 1;
			for (int i = startTag; i <= totalPage && i <= endTag; i++)
			{
				if (currentPage == i)
					sb.append("<li class=\"paginate_button active\"><a>"+i+"</a></li>\n");
				else
					sb.append("<li class=\"paginate_button\"><a onclick=\"nextPage("+i+")\">"+i+"</a></li>\n");
			}
			if (currentPage == totalPage)
			{
				sb.append("	<li class=\"paginate_button disabled\"><a>下页</a></li>\n");
				sb.append("	<li class=\"paginate_button disabled\"><a>尾页</a></li>\n");
			} else
			{
				sb.append("	<li class=\"paginate_button\"><a onclick=\"nextPage("+(currentPage+1)+")\">下页</a></li>\n");
				sb.append("	<li class=\"paginate_button\"><a onclick=\"nextPage("+totalPage+")\">尾页</a></li>\n");
			}
			
			sb.append("<li class=\"paginate_button\"><a>第"+currentPage+"/"+totalPage+"页</a></li>\n");
			sb.append("<li class=\"paginate_button\"><a>共<font color=red>"+totalResult+"</font>条</a></li>\n");
			sb.append("<li class=\"paginate_button\"><input type=\"number\" value=\"\" id=\"toGoPage\" style=\"width: 55px; text-align: center; float: left\" placeholder=\"页码\" /></li>\n");	
			sb.append("<li class=\"paginate_button\"><a onclick=\"jumpPage();\"	class=\"btn btn-mini btn-wirth btn-info\">跳转</a></li>\n");
			
			sb.append("<li class=\"paginate_button\"><select class=\"form-control\"  title=\"显示条数\" style=\"width: 55px; float: left;\"	onchange=\"changeCount(this.value)\">\n");
			sb.append("	<option value='"+showCount+"'>"+showCount+"</option>\n");
			sb.append("	<option value='5'>5</option>\n");
			sb.append("	<option value='10'>10</option>\n");
			sb.append("	<option value='20'>20</option>\n");
			sb.append("	<option value='30'>30</option>\n");
			sb.append("	<option value='40'>40</option>\n");
			sb.append("	<option value='50'>50</option>\n");
			sb.append("	<option value='60'>60</option>\n");
			sb.append("	<option value='70'>70</option>\n");
			sb.append("	<option value='80'>80</option>\n");
			sb.append("	<option value='90'>90</option>\n");
			sb.append("	<option value='99'>99</option>\n");
			sb.append("	</select>\n");
			sb.append("	</li>\n");

			sb.append("</ul>\n</div>\n</div>");
			sb.append("<script type=\"text/javascript\">\n");

			// 换页函数
			sb.append("function nextPage(page){");
			//sb.append(" window.parent.jzts();");
			sb.append("	if(document.forms[0].getAttribute(\"action\") == null){\n");
			sb.append("		var url = document.location+'';\n");
			sb.append("		if(url.indexOf('?')>-1){\n");
			sb.append("			var pos = url.indexOf('currentPage');\n");
			sb.append("			if(pos >-1){\n");
			sb.append("				url = url.substring(0,(pos+12));\n");
			sb.append("			}else{\n");
			sb.append("				url += \"&"
					+ (entityOrField ? "currentPage" : "page.currentPage")
					+ "=\";\n");
			sb.append("			}\n");
			sb.append("		}else{url += \"?"
					+ (entityOrField ? "currentPage" : "page.currentPage")
					+ "=\";}\n");
			sb.append("		url = url + page + \"&"
					+ (entityOrField ? "showCount" : "page.showCount") + "="
					+ showCount + "\";\n");
			sb.append("		document.location = url;\n");
			sb.append("	}else{\n");
			sb.append("		var url = document.forms[0].getAttribute(\"action\");\n");
			sb.append("		if(url.indexOf('?')>-1){url += \"&"
					+ (entityOrField ? "currentPage" : "page.currentPage")
					+ "=\";}\n");
			sb.append("		else{url += \"?"
					+ (entityOrField ? "currentPage" : "page.currentPage")
					+ "=\";}\n");
			sb.append("		url = url + page + \"&"
					+ (entityOrField ? "showCount" : "page.showCount") + "="
					+ showCount + "\";\n");
			sb.append("		document.forms[0].action = url;\n");
			sb.append("		openLoading();\n");
			sb.append("		document.forms[0].submit();\n");
			sb.append("	}\n");
			sb.append("}\n");

			// 调整每页显示条数
			sb.append("function changeCount(value){");
		//	sb.append(" window.parent.jzts();");
			sb.append("	if(document.forms[0].getAttribute(\"action\") == null){\n");
			sb.append("		var url = document.location+'';\n");
			sb.append("		if(url.indexOf('?')>-1){\n");
			sb.append("			var pos = url.indexOf('currentPage');\n");
			sb.append("			if(pos >-1){\n");
			sb.append("				url = url.substring(0,(pos+12))+\"1\";\n");
			sb.append("			}else{\n");
			sb.append("				url += \"1&"
					+ (entityOrField ? "currentPage" : "page.currentPage")
					+ "=1\";\n");
			sb.append("			}\n");
			sb.append("		}else{url += \"?"
					+ (entityOrField ? "currentPage" : "page.currentPage")
					+ "=1\";}\n");
			sb.append("		url = url + \"&"
					+ (entityOrField ? "showCount" : "page.showCount")
					+ "=\"+value;\n");
			sb.append("		document.location = url;\n");
			sb.append("	}else{\n");
			sb.append("		var url = document.forms[0].getAttribute(\"action\");\n");
			sb.append("		if(url.indexOf('?')>-1){url += \"&"
					+ (entityOrField ? "currentPage" : "page.currentPage")
					+ "=\";}\n");
			sb.append("		else{url += \"?"
					+ (entityOrField ? "currentPage" : "page.currentPage")
					+ "=\";}\n");
			sb.append("		url = url + \"1&"
					+ (entityOrField ? "showCount" : "page.showCount")
					+ "=\"+value;\n");
			sb.append("		document.forms[0].action = url;\n");
			sb.append("		openLoading();\n");
			sb.append("		document.forms[0].submit();\n");
			sb.append("	}\n");
			sb.append("}\n");

			// 跳转函数
			sb.append("function jumpPage(){");
			sb.append("var toPaggeVlue = document.getElementById(\"toGoPage\").value;");
			sb.append("if(toPaggeVlue == ''){document.getElementById(\"toGoPage\").value=1;return;}");
			sb.append("if(isNaN(Number(toPaggeVlue))){document.getElementById(\"toGoPage\").value=1;return;}");
			sb.append("nextPage(toPaggeVlue);");
			sb.append("}\n");
			sb.append("</script>\n");
		}
		page_html = sb.toString();
		return page_html;
	}


	public int getShowCount()
	{
		return showCount;
	}

	public void setShowCount(int showCount)
	{

		this.showCount = showCount;
	}

	public int getCurrentResult()
	{
		currentResult = (getCurrentPage() - 1) * getShowCount();
		if (currentResult < 0)
			currentResult = 0;
		return currentResult;
	}

	public void setCurrentResult(int currentResult)
	{
		this.currentResult = currentResult;
	}

	public boolean isEntityOrField()
	{
		return entityOrField;
	}

	public void setEntityOrField(boolean entityOrField)
	{
		this.entityOrField = entityOrField;
	}
	public PageData getPd()
	{
		return pd;
	}

	public void setPage_html(String page_html)
	{
		this.page_html = page_html;
	}

	public void setPd(PageData pd)
	{
		this.pd = pd;
	}

}
